﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using TuKu.Load;

namespace TuKu
{
    public partial class CategorySub : Form
    {
        public CategorySub()
        {
            InitializeComponent();
        }


        private void CategorySub_Load(object sender, EventArgs e)
        {
            DataBindClass.GroupBind(listGroup);
            listGroup.SelectedIndex = 0;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (listGroup.SelectedIndex > -1 && !String.IsNullOrEmpty(txtnames.Text))
            {
                string title = listGroup.SelectedItem.ToString();
                int groupid = int.Parse(new BLL.Group().GetModel(title).ID.ToString());
                char[] splite = { ',' };
                string[] names = txtnames.Text.Split(splite);
                Model.subGroup model = new Model.subGroup();

                LoadingHandler.Show(this, args =>
                {

                    foreach (string str in names)
                    {
                        if (!String.IsNullOrEmpty(str))
                        {
                            model.AddTime = DateTime.Now;
                            model.GroupID = groupid;
                            model.OrderID = 0;
                            model.Pin = Common.Hz2Py.Convert(str);
                            model.Title = str.Trim();
                            if (new BLL.subGroup().GetList(" title = '" + str + "' and groupid=" + groupid.ToString() + " ").Tables[0].Rows.Count < 1)
                            {

                                new BLL.subGroup().Add(model);
                                Thread.Sleep(50);
                            }
                        }
                    }
                });
                subGroupRefresh();
            }
            listGroup.Enabled = true;
        }

        private void listGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            subGroupRefresh();
        }
        private void subGroupRefresh()
        {
            int groupid = int.Parse(new BLL.Group().GetModel(listGroup.Text).ID.ToString());
            DataBindClass.subGroupBind(groupid, listBoxSubGroup);
        }

        private void listBoxSubGroup_DoubleClick(object sender, EventArgs e)
        {
            listBoxSubGroup.Height = listBoxSubGroup.Height - 24;
            txtModify.Visible = true;
            txtModify.Text = listBoxSubGroup.Text;
            btnModify.Visible = true;
            btndelete.Visible = true;
            listBoxSubGroup.Enabled = false;

        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            string name = txtModify.Text.Trim();
            if (!String.IsNullOrEmpty(name))
            {
                Model.subGroup model = new BLL.subGroup().GetModel(listBoxSubGroup.SelectedItem.ToString());
                if (((Button)sender).Text.Contains("修改"))
                {
                    model.Title = name;
                    model.Pin = Common.Hz2Py.Convert(name);
                    new BLL.subGroup().Update(model);
                }
                else
                {

                    if (MessageBox.Show("删除分类，该分类下的列表也被删除\n确认删除吗?", "确保", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        new BLL.subGroup().Delete(int.Parse(model.ID.ToString()));
                        new BLL.Topic().Delete(" subgroupid=" + model.ID.ToString());
                    }
                }
                listBoxSubGroup.Enabled = true;
                btnModify.Visible = false;
                txtModify.Visible = false;
                btndelete.Visible = false;
                listBoxSubGroup.Height = listBoxSubGroup.Height + 24;


                subGroupRefresh();
            }
        }

    }
}
